-- Copyright (C) 2013
--vss.workflow.upload is not OK

-------1定义公共组件(开�?---------------

local cjson = require "cjson"
local storage    = require("resty.gcsdfs.storage")              --加载storage �?
local iconv = require("iconv")
local utils   = require "gcs.utils"
local str_sub  = string.sub
local str_find = string.find
local str_len  = string.len
local tb_concat  = table.concat
local tb_getn = table.getn
local tb_insert = table.insert
local table = table
local type  = type
local pairs = pairs
local ngx = ngx
local os  = os
local string = string
local math_floor = math.floor
local math_random   =math.random
local tostring  = tostring
local tonumber  = tonumber
local cjson = require "cjson"
local error_table = ngx.shared.gcs_error   --声明ngx.shared.gcs_error
local config_table             = ngx.shared.gcs_config            --定义gcs_config缓存

-------1定义公共组件（结束）----------------
module(...)
local version = '0.1'


---------------------------------------------
----开�?---


function get_version()
  return version
end
--------------

--------接收二进制文件流转换成16进制
function _to16hex(_2hex)
  local tb_hex = {}
  for i = 1, string.len(_2hex) do
    local charcode = tonumber(string.byte(_2hex, i, i));
    local hexstr = string.format("%02X", charcode);
    tb_hex[i] = hexstr
  end
  local _16hex = table.concat(tb_hex)
  return _16hex
end
------------------接收16进制流按字节存入table中并剔除 无效的字节00
function _to16tb_hex(_16hex)
  local tb_bytes = {}
  local mn = 0
  for j=1,string.len(_16hex),2 do
    mn = mn+1
    tb_bytes[mn] = string.sub(_16hex,j,j+1)
  end
  --去掉table中的16进制00
  local i = 1
  while i <= #tb_bytes do
    if tb_bytes[i] == "00" then
      table.remove(tb_bytes,i)
    else
      i = i + 1
    end
  end
  return tb_bytes
end


-------------------接收16进制table流按字节转成10进制,放入table中
function _to10tb_hex(_tb16hex)
  local _10tb_hex = {}
  local _chex = {A=10,B=11,C=12,D=13,E=14,F=15}
  for i,v in pairs(_tb16hex) do
    local str1 = string.sub(string.upper(v),1,1)
    local str2 = string.sub(string.upper(v),2,2)
    local int_str1 = _chex[str1] or tonumber(str1)
    local int_str2 = _chex[str2] or tonumber(str2)
    _10tb_hex[i] = tonumber(int_str1)*16+tonumber(int_str2)
  end
  return _10tb_hex
end

------------------接收16进制table流按字节转成10进制,放入table中至少保留2位数字,例如9写成09

function _to10tb_hex1(_tb16hex)
  local _10tb_hex = {}
  local _chex = {A=10,B=11,C=12,D=13,E=14,F=15}
  for i,v in pairs(_tb16hex) do
    local str1 = string.sub(string.upper(v),1,1)
    local str2 = string.sub(string.upper(v),2,2)
    local int_str1 = _chex[str1] or tonumber(str1)
    local int_str2 = _chex[str2] or tonumber(str2)
    _10tb_hex[i] = tonumber(int_str1)*16+tonumber(int_str2)
    local _str_10hex = tostring(_10tb_hex[i])
    if string.len(_str_10hex) == 1 then
      _10tb_hex[i] = "0" .. _str_10hex
    end
  end
  return _10tb_hex
end









